package com.licode.codekit.utils;

import com.intellij.util.ResourceUtil;
import com.licode.codekit.buildprj.CodeKitConf;
import com.licode.codekit.buildprj.ProjectContext;
import com.licode.codekit.buildprj.ProjectCreator;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author licode
 */
public class FileUtil {
    public static String trimPath(String path) {
        if (null == path) {
            return path;
        } else {
            return path.replace("\\", "/")
                    .replace("//", "/");
        }
    }

    public static String findDirByServiceTag(File file) {
        String pkgDir = "";
        File[] fList;

        if (null == file) {
            return "";
        }

        fList = file.listFiles();

        if (null == fList) {
            return "";
        }

        for (File f : fList) {
            String name = f.getName();
            if (f.isDirectory()) {
                pkgDir = findDirByServiceTag(f);
            } else if (f.isFile()) {
                if (name.equals(CodeKitConf.PACKAGE_LOCATION_TAG)) {
                    pkgDir = file.getAbsolutePath() + "/";
                    break;
                }
            }
        }

        return pkgDir;
    }

    public static InputStream getRootResourceAsStream(String fileName) {
        return ResourceUtil.getResourceAsStream(FileUtil.class.getClassLoader(), "", fileName);
    }

    public static InputStream getResourceAsStream(String fileName) {
        return ResourceUtil.getResourceAsStream(FileUtil.class.getClassLoader(),
                CodeKitConf.TEMP_BASE_PATH, fileName);
    }

    public static String readText(InputStream inputStream, String charset) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            int _byte;
            while ((_byte = inputStream.read()) != -1) {
                byteArrayOutputStream.write(_byte);
            }

            String result = byteArrayOutputStream.toString(charset);
            byteArrayOutputStream.close();
            return result;
        } catch (Exception var6) {
            return var6.toString();
        }
    }

    public static String readFileContent(String filepath) {
        FileInputStream fis = null;
        String content = "";
        try {
            fis = new FileInputStream(filepath);
            int readBytes = 0;
            byte[] buffer = new byte[1024];
            StringBuilder sb = new StringBuilder();
            while ((readBytes = fis.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, readBytes, StandardCharsets.UTF_8));
            }
            fis.close();

            content = sb.toString();
        } catch (Exception ignore) {

        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return content;
    }

    public static void saveText(InputStream inputStream, String targetFile) {
        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            fileOutputStream = new FileOutputStream(targetFile);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            int _byte;
            byte[] buff = new byte[1024];
            while ((_byte = inputStream.read(buff)) != -1) {
                bufferedOutputStream.write(buff, 0, _byte);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bufferedOutputStream) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != fileOutputStream) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void saveStrToFile(String filepath, byte[] content) {
        File file = new File(filepath);
        saveStrToFile(file, content);
    }

    public static void saveStrToFile(File filepath, byte[] content) {
        FileOutputStream fileOutputStream;
        BufferedOutputStream bufferedOutputStream;

        try {
            fileOutputStream = new FileOutputStream(filepath);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            bufferedOutputStream.write(content);
            bufferedOutputStream.close();
            fileOutputStream.close();
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
    }

    public static void makeDir(String filePath) {
        if (!new File(filePath).mkdir()) {
            System.out.println("创建目录失败：" + filePath);
        }
    }

    public static void makeDir(File file) {
        if (!file.mkdir()) {
            System.out.println("创建目录失败：" + file.getAbsolutePath());
        }
    }

    public static void makeDirs(File file) {
        if (!file.mkdirs()) {
            System.out.println("创建多级目录失败：" + file.getAbsolutePath());
        }
    }

    public static void makeDirs(String filePath) {
        if (!new File(filePath).mkdirs()) {
            System.out.println("创建多级目录失败：" + filePath);
        }
    }

    public static void deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.exists() && !file.delete()) {
            System.out.println("删除文件失败：" + filePath);
        }
    }

    public static boolean pathExist(String filePath) {
        return new File(filePath).exists();
    }
}
